package com.stary.music.util;

import java.util.ResourceBundle;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 读取properties文件工具类
 * @author stary
 * @date 2018年6月13日 上午11:20:51
 */
public class PropertiesUtils {
	private static final Logger logger=LoggerFactory.getLogger(PropertiesUtils.class);
	
	private static ResourceBundle resource;
	/**
	 * 文件名
	 */
	private static String fileName="api.properties";	
	/**
	 * key前缀
	 */
	private String keyPrefix;
	/**
	 * key值
	 */
	private String key;
	
	static{
		if(fileName.contains(".properties")){
			fileName=fileName.split(".properties")[0];
		}
		resource = ResourceBundle.getBundle(fileName); // 读取属性文件内容	
	}

	public String getKeyPrefix() {
		return keyPrefix;
	}
	public void setKeyPrefix(String keyPrefix) {
		this.keyPrefix = keyPrefix;
	}
	public String getKey() {		
		return key;
	}
	public void setKey(String key) {
		if(!keyPrefix.isEmpty() && !keyPrefix.isEmpty()){
			key=keyPrefix+"."+key;
		}
		this.key = key;
	}
	/**
	 * 获取String值
	 * @param key
	 * @return
	 */
	public String getStringValue(){
		if(key!=null && key.isEmpty()){
			logger.error("key is null");
			return null;
		}
		if(!resource.containsKey(key)){
			logger.error("'{}' does not exist",key);
			return null;
		}

		return resource.getString(key);				

	}
	/**
	 * 获取Integer值
	 * @param key
	 * @return
	 */
	public Integer getIntegerValue(){
		String value=getStringValue();
		if(value!=null && !value.isEmpty()){
			return Integer.parseInt(value);
		}
		return null;				
		
	}	
}
